package com.csd.helper;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.csd.pojo.DocumentRecord;
import com.csd.pojo.Project;

public class Tool {
	public static List setToList(Set set) {
		List list = new ArrayList();
		list.addAll(set);
		return list;
	}

	public static Set listToSet(List list) {
		Set set = new HashSet();
		set.addAll(list);

		return set;
	}

	/*
	 * public static String dateToString(Date date ){
	 * 
	 * DateFormat sdf=new SimpleDateFormat("yyyy-MM-dd "); String a2 = null; a2
	 * = sdf.format(date); return a2; }
	 * 
	 * public static Date getCurrentDate() { Calendar calendar
	 * =Calendar.getInstance(); int year = calendar.get(calendar.YEAR); int
	 * month = calendar.get(calendar.MONTH); int day =
	 * calendar.get(calendar.DAY_OF_MONTH); //
	 * DateFormat.getDateInstance().format();
	 * 
	 * Date date = new Date(year,month,day); return date; }
	 */
	public static String dateToString(Date date) {
        if(date == null){
        	return "时间未知";
        }
        	
		DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd ");
		String a2 = null;
		a2 = sdf.format(date);
		return a2;
	}

	public static Date getCurrentDate() {
		Calendar c = Calendar.getInstance();
		Date d = c.getTime();
		return d;
	}

	public static String getPath() {
		HttpServletRequest request = ServletActionContext.getRequest();
		String basePath = request.getSession().getServletContext().getRealPath(
				"/");
		return basePath;
	}

	public static boolean createFile(File oldFile, String path) {

		try {
			File file = new File(path.substring(0, path
					.lastIndexOf(File.separator)));
			file.mkdirs();
			file = new File(path);
			if (!file.exists()) {
				if (!file.createNewFile())
					return false;
			}

			FileInputStream fis = new FileInputStream(oldFile);
			FileOutputStream fos = new FileOutputStream(file);

			byte[] buf = new byte[1024];
			int i = 0;
			while ((i = fis.read(buf)) != -1) {
				fos.write(buf, 0, i);
			}

			fos.close();
			fis.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;

		}
		return true;

	}

	public static void main(String args[]) {
		System.out.println(Tool.getCurrentDate());

	}
}
